package com.hh.test.design.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy {
	public class methodInterceptor implements MethodInterceptor {

		public Object intercept(Object obj, Method arg1, Object[] args,
				MethodProxy methodProxy) throws Throwable {
			System.out.println("begin invoke " + arg1.getName());
			Object re = methodProxy.invokeSuper(obj, args);
			System.out.println("after invoke " + arg1.getName());

			return re;
		}

	}

	public static void main(String[] args) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserDao.class);
		enhancer.setCallback(new CglibProxy().new methodInterceptor());
		UserDao dao = (UserDao) enhancer.create();
		System.out.println(dao.save("A"));

	}
}
